//
//  LoginViewController.m
//  BabyAndBabyDriver
//
//  Created by 范琦 on 16/8/10.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "LoginViewController.h"
#import "LogInView.h"
#import "MBProgressHUD.h"
#import "ForgetPasswordViewController.h"
@interface LoginViewController ()
{
    
    MBProgressHUD *hud;
    
}

@property (nonatomic, strong) LogInView *loginView;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"登录";
    [self createLoginView];
}

- (void)createLoginView
{
    self.loginView = [[[NSBundle mainBundle] loadNibNamed:@"LogInView" owner:nil options:nil] lastObject];
    _loginView.frame = CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT);
    _loginView.firstView.layer.borderWidth = 0.5;
    _loginView.firstView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _loginView.firstView.layer.masksToBounds = YES;
    _loginView.firstView.layer.cornerRadius = 5;
    
    _loginView.secondView.layer.borderWidth = 0.5;
    _loginView.secondView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _loginView.secondView.layer.masksToBounds = YES;
    _loginView.secondView.layer.cornerRadius = 5;
    [self.view addSubview:_loginView];
    
    [[_loginView.forgetpasswordButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        ForgetPasswordViewController *forget = [[ForgetPasswordViewController alloc] init];
        [self.navigationController pushViewController:forget animated:YES];
    }];
    
    [[_loginView.makesureButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        YTKNetHandle *api = [[YTKNetHandle alloc]initWithUrl:@"/Auth/login" paramDic:@{@"username":@"15840681508", @"password":@"1111"} RegisterApiMethod:RequestMethodPost];
        
        [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
            NSLog(@"%@", request.responseBody);
            [hud hide:YES];
            if ([request.responseBody[@"error"] integerValue] == 0) {
                
                [self showAlertView:@"登录成功" timeOut:1];
                
                NSDictionary *dic = request.responseBody[@"result"];
                NSString *str = dic[@"token"];
                [[NSUserDefaults standardUserDefaults] setObject:str forKey:@"TOKEN"];
                
                [[NSUserDefaults standardUserDefaults]synchronize];
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"Login" object:nil];
                
            }else{
                
                
                [self showAlertView:request.responseBody[@"msg"] timeOut:1];
            }
        } failure:^(YTKBaseRequest *request) {
        }];
        
        
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
